import java.io.IOException;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;

import com.batchaps.global.GameEngine;
import com.batchaps.menu.MenuManager;

public class CanvasMain extends GameCanvas implements Runnable {

	Main midlet;
	MenuManager menuManager;
	Image imgbackground;
	int srcY = -80;

	public CanvasMain(final Main midlet) {
		super(true);
		setFullScreenMode(true);
		this.midlet = midlet;

		// constructor
		Init();
	}

	public void Init() {
		// init menumanager
		menuManager = new MenuManager(GameEngine.k_Screen_Width,
				GameEngine.k_Screen_Height);
		// int bacground
		try {
			imgbackground = Image.createImage("/background.jpg");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//
		Thread t = new Thread(this);
		t.start();
	}

	public void run() {
		// TODO Auto-generated method stub
		while (true) {
			
			srcY++;
			if(srcY > 0){
				srcY = -80;
			}
			
			menuManager.Update();

			Draw();

			try {
				Thread.sleep(20);
			} catch (InterruptedException ex) {
				ex.printStackTrace();
			}
		}
	}

	protected void pointerReleased(int arg0, int arg1) {
		// TODO Auto-generated method stub
		if (menuManager.IsClick(arg0, arg1) == 5)
			midlet.Exit();
	}

	protected void pointerPressed(int arg0, int arg1) {
		// TODO Auto-generated method stub
		menuManager.pointerPress(arg0, arg1);
	}

	public void Draw() {
		Graphics g = getGraphics();

		Image temp = Image.createImage(GameEngine.k_Screen_Width,
				GameEngine.k_Screen_Height);
		Graphics gtemp = temp.getGraphics();
		gtemp.drawImage(imgbackground, 0 , srcY  , 0);
		menuManager.Draw(gtemp);

		g.drawImage(temp, 0, 0, 0);
		flushGraphics();
	}

}
